#!/bin/bash

########################################################################
# This file is released under the Creative Commons Attribution-Share 
# Alike 3.0 License
#
# All enclosed files are copyright (C) 2007 Mike Jones & FlashGen.Com, 
# unless otherwise indicated.
#
# You can redistribute it and/or modify it under the terms of the 
# CREATIVE COMMONS ATTRIBUTION-SHARE ALIKE 3.0 License as published by 
# the Creative Commons.
#
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#
# See the Creative Commons Attribution-Share Alike 3.0 License for 
# more details.
#
# For any queries not directly related to the CREATIVE COMMONS 
# ATTRIBUTION-SHARE ALIKE 3.0 LICENSE please use the following methods 
# of contact:
#
# web: http://www.flashgen.com
# email: fgdevelopment@flashgen.com
#
########################################################################

########################################################################
					# Set up our variables #
########################################################################

# Here we store our parameters that get passed in by the Ant task, or the
# command line (if you want to use it that way).

# The project directory - This is used for the symlink so it should be the 
# root of your project directory
projectPath="$1"
sourceDir="$2"
outputDir="$3"
templateDir="$4"

# These variables deal with setting the various titles in the actual docs
windowTitle="$5"
mainTitle="$6"
footer="$7"

# Here we store the location of ASDoc 
asdocPath="$8"

# Here we set the symlink path - By default this is set to your documents 
# directory and the symlink gets hidden automatically. If you want to 
# change this path make sure you leave it quote free like below.
tempLink=~/Documents/.ASDocSymLink

# This function just sets the symlink and checks to see if one is already 
# present  - if it is it gets deleted first otherwise you get a bit of a
# symlink nesting issue. 
# Note: If you want to remove the symlink rom your system just run the
# remove symlink Ant task.
function setSymLinks()
{
	# Check to see if the link is present  and if so remove it.
	if [ -d $tempLink ]
		then
			rm $tempLink
	fi	

	# Create the symlink ready to run our ASDoc calls to generate the 
	# documentation.
	ln -fs "$projectPath" "$tempLink"
	
	# Now we have our symlink in place we can create our documentation.
	createDocuments
}

# This function is the guts of the ASDoc creation. Now we have our symlink
# we can use it to create our documents without the headache of spaces in 
# our path names.
function createDocuments()
{
	echo "Using Source directory: $projectPath/$sourceDir"
	
	# Bootstrap the output directory (if none is supplied then it will 
	# create it in the same folder as the Any scripts in a folder called 
	# "asdoc-ouput"
	if [ -n "$outputDir" ]
		then
			echo "Using $projectPath/$outputDir as directory for documentation"
			output="$tempLink/$outputDir"
		else
			echo "No output directory specified using: $projectPath/docs"
			output="$tempLink/docs"		
	fi
	
	# Bootstrap the template directory in case the user omits it
	if [ -n "$templateDir" ]
		then
			echo "Using Template directory: $templateDir"
			template="$templateDir"
		else
			echo "No template directory specified using the default: $asdocPath/asdoc/templates"
			template="$asdocPath/asdoc/templates"			
	fi
	
	# Check to see if the document directory already exists. If it does we 
	# remove it.	
	if [ -d "$output" ]
		then
			echo -e "Deleting old document directory"
			rm -Rf "$output"
	fi
	
	# Regardless of wheter there was a directory for the documentation to remove 
	# (see above). We still need to create one.
	echo "Creating new document directory"
	mkdir "$output"
	
	# These two lines are basically identical to the asdoc shell script on Mac OSX 
	# I have placed them here to avoid having to make the end user do any 
	# alterations to the original scripts (as it is a bit of a pain and this way 
	# all the code is centralised here).
	
	VMARGS='-Xmx1024m -Dsun.io.useCanonCaches=false\
	 -Xbootclasspath/p:"$asdocPath"/asdoc/lib/xalan.jar'

	java $VMARGS -classpath "$asdocPath"/lib/asdoc.jar\
	 flex2.tools.ASDoc +flexlib="$asdocPath"/frameworks\
	  -doc-sources="$tempLink/$sourceDir" -output="$output"\
	  -templates-path="$template" -window-title="$windowTitle"\
	  -main-title="$mainTitle" -footer="$footer" | $suppressor
	
	# You will notice I pipe the output from the java command to a variable that is
	# aptly named "suppressor". This variable does nothing beyond supressing the 
	# return as I don't particulary like the output from asdoc.jar. To get the 
	# output just remove the pipe '|' and '$suppressor' from the script
	
	echo "Documentation creation complete"
	
	# Finally remove the symbolic link
	rm $tempLink
}

setSymLinks
exit $?